package xj.graph2d;

import static xj.graph2d.DrawShapeConstants.d;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import xj.graph2d.util.DrawingUtilities;

public class RectangularGroup extends Group {

  @Override
  public DrawShape makeInstance() {
    return new RectangularGroup();
  }

  @Override
  public void draw(Graphics2D g2) {
    super.draw(g2);
    if (drawGroupFrame) {
      DrawingUtilities.drawGroupFrame(g2, bounds, true);
    }
  }

  @Override
  public void drawOutline(Graphics2D g2) {
    super.drawOutline(g2);
    if (drawGroupFrame) {
      DrawingUtilities.drawGroupFrame(g2, bounds, false);
    }
  }

  @Override
  public void drawFrame(Graphics2D g2) {
    int penSize = 1;
    DrawAttr attr = getDrawAttr();
    if (attr != null) {
      penSize = attr.penSize;
    }
    DrawingUtilities.drawRectFrame(g2, bounds, needFrameRect, 
				   penSize + 2 * (int) d, groupFrameColor);
  }

  @Override
  public void setBounds(float x1, float y1, float x2, float y2) {
    super.setBounds(x1, y1, x2, y2);
    p1.setLocation(x1, y1);
    p2.setLocation(x1 + x2, y1 + y2);
  }

  @Override
  public boolean contains(DrawShape s) {
    if (s != null) {
      Rectangle2D r = s.getBounds();
      double x = r.getX();
      double y = r.getY();
      double w = r.getWidth();
      double h = r.getHeight();
      if (w != 0 && h != 0) {
	return bounds.contains(x, y, w, h);
      } else {
	return (bounds.contains(x, y) && bounds.contains(x + w, y + h));
      }
    }
    return false;
  }

}
